<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>tab--项目文件--上传图纸</title>
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta http-equiv="Expires" content="0">
    <meta http-equiv="Cache-Control" content="no-siteapp,no-transform,no-store, no-cache, must-revalidate">
    <meta http-equiv="Pragma" content="no-cache">
    <link rel="stylesheet" href="/lib/layui/css/layui.css">
    <link rel="stylesheet" href="/static/css/weadmin.css">
    <link rel="stylesheet" href="./../../../lib/jqueryZtree/3.5.12/css/zTreeStyle/zTreeStyle.min.css">
    <script type="text/javascript" src="/lib/jquery/jquery.js" charset="utf-8"></script>
    <script type="text/javascript" src="./../../../lib/jqueryZtree/3.5.12/js/jquery.ztree.all-3.5.js"
            charset="utf-8"></script>
    <style>
        .leftBox {
            float: left;
            width: 25%;
            height: 100%;
            border-right: 2px solid #eee;
            -webkit-box-sizing: border-box;
            -moz-box-sizing: border-box;
            box-sizing: border-box;
        }

        .rightBox {
            width: 75%;
            float: left;
            height: 100%;
        }

        .ztree {
            height: 100%;
            -webkit-box-sizing: border-box;
            -moz-box-sizing: border-box;
            box-sizing: border-box;
        }

        #tuzhiTree {
            height: 100%;
            overflow-x: auto;
        }
    </style>
</head>
<body>
<p class="stageBox" style="font-size: 14px;font-weight: bold;">
    <span>流程当前办理阶段:</span>
    <span class="stage" style="color: #0000FF"></span>
</p>
<div class="profileBox">
    <div class="leftBox">
        <div id="tuzhiTree" class="ztree"></div>
    </div>
    <div class="rightBox">
        <table class="layui-hide" id="dataList" lay-filter="dataList"></table>
        <script type="text/html" id="toolbarOrg">
            <div class="layui-btn-container shangchaun">
                <button class="layui-btn" lay-event="uploadBtn">上传</button>
                <button class="layui-btn" lay-event="download">打包下载</button>
                <button class="layui-btn" lay-event="delete">删除</button>
            </div>
        </script>
        <script type="text/html" id="barDemo">
            <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
        </script>
    </div>
</div>

<script type="text/javascript" src="/lib/layui/layui.js"></script>
<script src="/static/js/main.js"></script>
<script>
    layui.use(['jquery', 'ztreeBase', 'sessionFun', 'configureCom', 'configure', 'table', 'jsDictFile', 'allHeight'], function () {
        var ztreeBase = layui.ztreeBase;
        var sessionFun = layui.sessionFun;
        var table = layui.table;
        var jsDictFile = layui.jsDictFile;
        var configureCom = layui.configureCom;
        var configure = layui.configure;
        var allHeight = layui.allHeight;
        allHeight.init('.stageBox', '.profileBox');
        var stage = sessionFun.get(configure.SESSION_PROJECT_WAIT_LIST_DATA);
        $('.stage').html(stage.taskDefName);
        var listData = sessionFun.get(configure.SESSION_PROJECT_WAIT_LIST_DATA);
        //项目文件初始化
        fileInit();

        //获取图纸树---传入的id代表着被选中的节点，默认选中展开
        function fileInit(id) {
            id = id || '';
            configureCom.postDataAjax(configure.PROJECT_FILE_TREE_LIST_HTTP, {decId: listData.decId}).then(function (res) {
                //将数据格式转变，自定义第二级图标
                $(res.result).each(function (index, item) {
                    $(item.children).each(function (i, p) {
                        if (p.children.length > 0) {
                            p.iconSkin = 'pIcon01';
                        }
                    });
                });
                ztreeBase.param.idKey = 'id';//父级id
                ztreeBase.param.pIdKey = 'parentId';//子级中与父级id关联
                ztreeBase.param.rootPId = '57e227066d98436fbf56abb8a91cbb82';//根节点id
                ztreeBase.init('tuzhiTree', id, res.treeNodeList);
            });
        }

        //获取专业id-递归方法
        function getSpecialtyId(node) {
            if (node == null) {
                return '';
            }
            var returnVal = '';
            $(jsDictFile.ALL_SPE[0].childrens).each(function (index, item) {
                if (item.value == node.code) {
                    returnVal = node.code;
                    return false;
                }
            });
            if (returnVal) {
                return node.code;
            } else {
                return getSpecialtyId(node.getParentNode());
            }
        }

        //获取当前节点的根节点(treeNode为当前节点)
        function getCurrentRoot(treeNode) {
            if (treeNode.getParentNode() != null) {
                var parentNode = treeNode.getParentNode();
                return getCurrentRoot(parentNode);
            } else {
                return treeNode;
            }
        }

        //树形图回调
        ztreeBase.param.onClick = function (event, treeId, treeNode, path) {
            treeNode.drawingId = treeNode.id;
            if (treeNode.children.length == 0) {//可点击均为最里层元素，children内没有任何元素
                treeNode.path = path;
                treeNode.specialtyId = getSpecialtyId(treeNode);  //专业code
                sessionFun.set(configure.SESSION_FILE_TREE_DATA, treeNode);//存储文件树选中对象
                var currentRoot = getCurrentRoot(treeNode);//获取当前节点的根节点
                var parentNode = treeNode.getParentNode();//获取当前节点的父节点
                var toolBarOrg = 'toolbarOrg';
                if (configure.KC_SPEC_CODE.indexOf(currentRoot.code) > -1) {//如果当前节点是勘察专业
                    if (!configureCom.orgTypeFun(configure.KC_CODE)) {//如果当前登录账号不是勘察类型
                        toolBarOrg = true;
                    }
                }
                if (configure.SJ_SPEC_CODE.indexOf(currentRoot.code) > -1) {//如果当前节点是设计专业
                    if (!configureCom.orgTypeFun(configure.SJ_CODE)) {//如果当前登录账号不是设计类型
                        toolBarOrg = true;
                    }
                }
                if (!configureCom.orgTypeFun(configure.JSDW_CODE)) {//如果当前登录账号不是建设类型
                    if (configure.PRJ_JSDW_CODE.indexOf(parentNode.code) > -1) {//如果当前节点不是项目批文与单位证明--建设单位
                        toolBarOrg = true;
                    }
                }
                if (!configureCom.orgTypeFun(configure.KC_CODE)) {//如果当前登录账号不是勘察类型
                    if (configure.PRJ_KCDW_CODE.indexOf(parentNode.code) > -1) {//如果当前节点不是项目批文与单位证明--勘察单位
                        toolBarOrg = true;
                    }
                }
                if (!configureCom.orgTypeFun(configure.SJ_CODE)) {//如果当前登录账号不是设计类型
                    if (configure.PRJ_SJDW_CODE.indexOf(parentNode.code) > -1) {//如果当前节点不是项目批文与单位证明--设计单位
                        toolBarOrg = true;
                    }
                }
                if (!configureCom.orgTypeFun(configure.STDW_CODE)) {//如果当前登录账号不是审图类型
                    if (configure.PRJ_SCDW_CODE.indexOf(parentNode.code) > -1) {//如果当前节点不是项目批文与单位证明--审图单位
                        toolBarOrg = true;
                    }
                }
                if (configure.PRJ_KCHT_CODE.indexOf(treeNode.code) > -1) {//如果当前节点是勘察合同
                    if (!configureCom.orgTypeFun(configure.KC_CODE)) {//如果当前登录账号不是勘察类型
                        toolBarOrg = true;
                    }
                }
                if (configure.PRJ_SJHT_CODE.indexOf(treeNode.code) > -1) {//如果当前节点是设计合同
                    if (!configureCom.orgTypeFun(configure.SJ_CODE)) {//如果当前登录账号不是设计类型
                        toolBarOrg = true;
                    }
                }

                if (configure.PRJ_SCHT_CODE.indexOf(treeNode.code) > -1) {//如果当前节点是审查合同
                    if (!configureCom.orgTypeFun(configure.STDW_CODE)) {//如果当前登录账号不是审图类型
                        toolBarOrg = true;
                    }
                }
                configureCom.tableRender(configure.PROJECT_FILE_LIST_HTTP, {dirId: treeNode.id}, cols, 'dataList', 10, '', toolBarOrg);
            }
        };
        var cols = [[
            {type: 'checkbox'},
            {
                field: 'name', title: '附件名'
            },
            {title: '操作', toolbar: '#barDemo', width: 170}
        ]];

        //头工具栏事件
        table.on('toolbar(dataList)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            var fileTreeData = sessionFun.get(configure.SESSION_FILE_TREE_DATA);
            sessionFun.remove(configure.SESSION_CKTZ_INFO_DATA);
            switch (obj.event) {
                case 'uploadBtn':
                    top.layer.open({
                        type: 2,
                        title: '图纸上传',
                        content: configure.,
                        area: configureCom.Psize1,
                        cancel: function (index, layero) {
                            fileInit(fileTreeData.id);
                            configureCom.tableReload();
                        }
                    });
                    break;
                case 'delete':
                    if (!checkStatus.data.length) {
                        top.layer.alert('请您至少选择一条数据后再提交！');
                        return false;
                    }
                    top.layer.confirm('您真的要删除选中的数据吗？', function (index, layero) {
                        top.layer.close(index);
                        var attIds = '';
                        $(checkStatus.data).each(function (index, item) {
                            if (!index) {
                                attIds = item.id;
                            } else {
                                attIds += ',' + item.id;
                            }
                        });
                        configureCom.postDataAjax(configure.PROJECT_FILE_DELETE_HTTP, {
                            decId: listData.decId,
                            path: fileTreeData.path,
                            attIds: attIds
                        }).then(function (res) {
                            top.layer.msg('删除成功！');
                            configureCom.tableReload();
                        })
                    });
                    break;
                case 'download':
                    if (checkStatus.data.length == 0) {
                        alert("请选择需要打包下载的文件");
                        return;
                    }
                    downloadFun(checkStatus.data, fileTreeData);
                    break;
            }
        });

        //下载
        function downloadFun(data, fileTreeData) {
            var fileNames = '';
            $(data).each(function (index, item) {
                if (!index) {
                    fileNames += item.name;
                } else {
                    fileNames += ',' + item.name;
                }
            });
            var url = configure.HOST + configure.DRAWING_ZIP_DOWNLOAD + '?decId=' + listData.decId + '&path=' + encodeURIComponent(fileTreeData.path) + '&fileNames=' + encodeURIComponent(fileNames) + '&token=' + configureCom.getToken();
            window.open(url);
        }


        //每行操作
        table.on('tool(dataList)', function (obj) {
            var data = obj.data;
            var fileTreeData = sessionFun.get(configure.SESSION_FILE_TREE_DATA);
            sessionFun.set(configure.SESSION_CKTZ_INFO_DATA, data);
            if (obj.event === 'detail') {
                top.layer.open({
                    type: 2,
                    title: '查看图纸',
                    area: configureCom.Psize1,
                    content: configure.CHECK_DRAWING_DRAWING_DRAWING_READ_HTML,
                    success: function (layero, index) {
                        top.layer.full(index);
                    }
                });
            } else if (obj.event === 'update') {
                top.layer.open({
                    type: 2,
                    title: '图纸更新',
                    content: configure.UPLOAD_HTML,
                    area: configureCom.Psize1,
                    cancel: function (index, layero) {
                        fileInit(fileTreeData.id);
                        configureCom.tableReload();
                    }
                });
            }
        });
    })
</script>
</body>
</html>